import { Component, OnInit } from '@angular/core';
import { JsonResult } from 'src/app/global/model';
import { Mill, MillService } from 'src/app/service/mill.service';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { UserMill, UserMillService } from 'src/app/service/user-mill.service';
import { ActivatedRoute, Router } from '@angular/router';
import { PlatformLocation } from '@angular/common';
import { AuditService, SysUser } from 'src/app/service/audit.service';

@Component({
  selector: 'app-audit',
  templateUrl: './audit.component.html',
  styleUrls: ['./audit.component.css'],
  providers: [NzNotificationService]
})
export class AuditComponent implements OnInit {

  mills:Mill[] = [];
  openId:string;
  nickName:string;
  realName:string;
  mill:string;


  constructor(
    public millService:MillService,
    private noti:NzNotificationService,
    private userMillService:UserMillService,
    private route:ActivatedRoute,
    private service:AuditService,
  ) { 
    this.getMills();
  }

  ngOnInit(): void {
    this.route.queryParams.subscribe( p => {
      // console.log(p);
      // console.log('openId=' + p['openId']);
      // console.log('昵称=' + p['nickName']);
      this.openId = p['openId'];
      this.nickName = p['nickName'];
      this.realName = p['nickName'];
    })
  }

  getMills(){
    this.millService.selectAllMill().subscribe(
      (jr:JsonResult) => {
        if( jr.code !=1 ){
          console.log(jr.msg);
          this.msg(jr.msg);
          return
        }

        this.mills = [...jr.data];
      },error => console.log('请求后端工厂信息时出现异常！')
    )
  }


  msg(content: string) {
    this.noti
    .blank('通知', content)
    .onClick.subscribe(() => {
      console.log('notification clicked!');
    });
  }

  save(event){
    // let data:UserMill = new UserMill();
    // data.open_id = this.openId;
    // data.mill_sid = this.mill;
    // this.userMillService.save(data).subscribe(
    //   (jr:JsonResult) => {
    //     if( jr.code !=1 ){
    //       console.log(jr.msg);
    //       this.msg(jr.msg);
    //       return
    //     }
    //     this.msg('保存完毕！');
    //   },error => console.log('保存用户工厂关系时出现异常！')
    // )

    this.service.audit(this.openId,this.realName,this.mill).subscribe(
      (jr:JsonResult) => {
        if( jr.code !=1 ){
          this.msg(jr.msg);
          return
        }

        this.msg('审核成功！');
      },error => console.log('请求后端审核时出现异常！')
    )
  }

  // 2021年6月6日 16:00:48
  // 新用户关注服务号后推送给管理员的待审核用模板消息中
  // 追加将用户添加到业务数据库中，即给业务数据库的表 sys_user 新增一行数据
  insertUser(event){
    let user:SysUser = new SysUser();
    user.openid = this.openId;
    this.service.insertUser(user).subscribe(
      (jr:JsonResult) => {
        if( jr.code !=1 ){
          this.msg(jr.msg);
          return
        }

        this.msg('为业务系统新增用户成功！');
        const sysUser:SysUser = jr.data;
        this.msg(SysUser.toString());
      },error => console.log('请求后端新增业务系统用户时出现异常！')
    )
  }

  // wechat.user_mill 写入数据
  saveUserMill(event){
    console.log(event);
    
    let record:UserMill = new UserMill();
    record.open_id = this.openId;
    record.mill_sid = this.mill;
    this.service.saveUserMill(record).subscribe(
      ( jr:JsonResult )=>{
        if( jr.code !=1 ){
          this.msg(jr.msg);
          return
        }

        this.msg('为微信用户关联mill成功！');

      },error => console.log('微信后台数据中写入微信用户关联mill时出现异常！')
    )
  }

}
